package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.dao.UserDao;
import com.baizhi.cmfz.entity.User;
import com.baizhi.cmfz.entity.UserProvinceDTO;
import com.baizhi.cmfz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserDao userDao;
    Map map = new HashMap();
    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Override
    @Transactional
    public Map selectUser(int page, int limit,String userName) {
        //将页码转换为偏移量
        int offset = (page-1)*limit;
        //对收到的参数userName进行数据的处理
        //当页面传来的数据不为空或者不为空字符串时 对数据模糊处理
        if (userName != null && !userName.equals("")) {
            userName = "%"+userName+"%";
        }else {
            userName = null;
        }

        //System.err.println("userName = " + userName);

        //查询当前页的数据
        List<User> users = userDao.selectUser(offset, limit,userName);
        //查询总条数
        int count = userDao.selectUserCount(userName);
        map.put("code",0);
        map.put("msg","");
        map.put("data",users);
        map.put("count",count);
        return map;
    }

    @Override
    @Transactional
    public List<Map> selectSex() {
        List<Map> maps = userDao.selectSex();
        //System.out.println(maps);
        return maps;
    }

    @Override
    @Transactional
    public Map selectprovince() {
        //long startTime = System.currentTimeMillis();
        List<UserProvinceDTO> userProvinces = userDao.selectprovince();
        Map map = new HashMap();
        for (int i = 0; i < userProvinces.size(); i++) {
            //System.out.println(userProvinces.get(i));
            String userProvince = userProvinces.get(i).getUserProvince();
            int count = userProvinces.get(i).getCount();
            map.put(userProvince,count);
        }
        /*long endTime = System.currentTimeMillis();
        long time = endTime-startTime;
        log.info(String.valueOf(time));*/
        return map;
    }

    @Override
    @Transactional
    public Map selectDate() {
        Map map = new HashMap();
        map.put("one",userDao.selectDate(0,7));
        map.put("two",userDao.selectDate(7,14));
        map.put("three",userDao.selectDate(14,21));
        //System.out.println(map);
        return map;
    }

    @Override
    public Map updateUserStatus(int userId, int userStatus, int onclickStatus) {
        logger.debug("用户id"+userId);
        logger.debug("用户状态"+userStatus);
        logger.debug("点击事件状态"+onclickStatus);
        //先进行当前点击事件状态判断
        if (onclickStatus ==0) {
            //说明点击的不是删除，在进行用户状态的判断
            if (userStatus == 0) {
                userStatus = 2;
            }else if (userStatus == 1) {
                userStatus =0;
            }else if (userStatus == 2) {
                userStatus = 0;
            }
        }else {
            //说明是删除
            userStatus = 3;
        }
        //执行修改操作
        int i = userDao.updateUserStatus(userId,userStatus);
        Map map = new HashMap();
        if (i == 1) {
            map.put("isUpdate",true);
        }else {
            map.put("isUpdate",false);
        }
        return map;
    }
}
